/**
 * Created by wangxiaodan on 2017/4/21.
 */
$(function(){
    getContent("/admin/home");  //初始化加载界面
    $('.treeview-menu li').click(function(){//点击li加载界面
        var url = $(this).attr('data-url');
        $(this).siblings().removeClass('active');
        $(this).addClass('active');
        getContent(url);
    });
});

//跳转按钮事件
function jumpfun(obj) {
    getContent($(obj).attr('data-url'));
}

//提交查询按钮
function postfun(obj) {
    $.post($(obj).attr("data-url"), $('form').serialize(), function (data) {
        $(".content").html(data);
    });
}

//提交按钮事件
function subAction(obj) {
    $.ajax({
        url: $(obj).attr("data-url"),
        type: $(obj).attr("data-type"),
        data: $('form').serialize(),
        dataType: "json",
        success: function (data) {
            if(data.code == 1){
                swal("操作成功!", data.msg, "success");
                getContent(data.url);
            }else {
                swal("操作失败!", data.msg, "error");
            }
        },
        error: function (msg) {
            var err = JSON.parse(msg.responseText);
            var error = "";
            for (var i in err){
                error += '<span style="color: #f27474;">' + err[i] + '<span>' + '<br />';
            }
            error = error.substring(0, error.length-6);
            swal({
                title: "错误",
                text: error,
                html: true,
                type: "error",
            });
        }
    });
}

function getContent(url) {
    $.get(url,function(data){
        $(".content").html(data);
    });
}

function delfun(obj) {
    swal({
            title: "确定删除?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "狠心删除",
            closeOnConfirm: false
        },
        function(){
            $.ajax({
                url: $(obj).attr('data-url'),
                type: "DELETE",
                dataType: "json",
                success: function(data) {
                    if(data.code == 1){
                        swal("操作成功!", "", "success");
                        $(obj).parent().parent().remove();
                    }else {
                        swal("操作失败!", data.msg, "error");
                    }
                }
            });
        })
}

function changefun(obj) {
    swal({
            title: "确定改变状态吗?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "执行",
            closeOnConfirm: false
        },
        function(){
            $.ajax({
                url: $(obj).attr('data-url'),
                type: "DELETE",
                dataType: "json",
                success: function(data) {
                    if(data.code == 1){
                        swal("操作成功!", "", "success");
                        getContent(data.url);
                    }else {
                        swal("操作失败!", "参数错误", "error");
                    }
                }
            });
        })
}

/**
 * 表示实现全选功能
 */
function setSelectAll(eleA, eleB) {
    eleA.on("click", function () {
        eleB.each(function () {
            this.checked = eleA.prop("checked");
        })
    })
}